package com.shxc.smarttrade.old.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.shxc.smarttrade.R;
import com.shxc.smarttrade.old.adapter.MasterListTwoAdapter;
import com.shxc.smarttrade.old.bean.Master;
import com.shxc.smarttrade.old.constants.URLContents;
import com.shxc.smarttrade.old.myview.MyFootView;
import com.shxc.smarttrade.old.myview.MyRefreshHeadView;
import com.shxc.smarttrade.old.myview.RecycleViewDivider;
import com.shxc.smarttrade.old.utils.CloseAllActivity;
import com.shxc.smarttrade.old.utils.MyOkHttpUtils;
import com.shxc.smarttrade.old.utils.SpUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * Created by jcdev1 on 2017/7/26.
 */

public class SupermanListActivity extends FragmentActivity {
    @BindView(R.id.tv_center)
    TextView tvCenter;
    @BindView(R.id.x_recycler_view)
    XRecyclerView xRecyclerView;
    private LinearLayoutManager manager;
    private MasterListTwoAdapter adapter;
    private List<Master.DataBean.MastersBean> allMasterList = new ArrayList<>();
    private int page = 1;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Master master = (Master) msg.obj;
                    if (master.getErr_code().equals("0")) {
                        List<Master.DataBean.MastersBean> mastersBeanList = master.getData().getMasters();
                        if (mastersBeanList.size() != 0) {
                            allMasterList.addAll(mastersBeanList);
                            adapter.setDatas(allMasterList);
                        } else {
                            if (xRecyclerView != null)
                                xRecyclerView.setNoMore(true);
                            showToast("已加载全部！");
                        }
                    } else {
                        showToast(master.getErr_msg());
                    }
                    xRecyclerView.refreshComplete();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_superman_list);
        CloseAllActivity.getScreenManager().pushActivity(this);
        initView();
        initListener();
        getData(page, 10);
    }

    private void initListener() {
        adapter.setMyItemCLickListener(new MasterListTwoAdapter.MasterTwoItemClickListener() {
            @Override
            public void ItemViewClickListener(Master.DataBean.MastersBean mb, int posintion) {
                Intent intent = new Intent(SupermanListActivity.this, SupermanDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("masterSBean", mb);
                bundle.putInt("where", 2);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        xRecyclerView.refreshComplete();
                    }
                }, 1000);
            }

            @Override
            public void onLoadMore() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page++;
                        getData(page, 10);
                    }
                }, 1000);

            }
        });
    }

    private void initView() {
        tvCenter.setText("高手排行榜");
        manager = new LinearLayoutManager(this);
        xRecyclerView.setLayoutManager(manager);
        xRecyclerView.addItemDecoration(new RecycleViewDivider(this, LinearLayout.VERTICAL, 20, getResources().getColor(R.color.all_background)));
        xRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        xRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallSpinFadeLoader);
        MyRefreshHeadView myRefreshHeadView = new MyRefreshHeadView(this);
        myRefreshHeadView.setArrowImageView(R.mipmap.indicator_arrow);
        xRecyclerView.setRefreshHeader(myRefreshHeadView);
        xRecyclerView.setFootView(new MyFootView(this));
        xRecyclerView.setAdapter(adapter = new MasterListTwoAdapter(this));
    }

    private void getData(int page, int pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("accessToken", SpUtils.getString(this, "accessToken", ""));
        map.put("page", page + "");
        map.put("pageSize", pageSize + "");
        MyOkHttpUtils.getServiceDatas(this,map, URLContents.SUPER_MAN_LIST, new MyOkHttpUtils.ServiceStatus() {
            @Override
            public void faild(Call call, Exception e, int id) {
                showToast("网络异常");
            }

            @Override
            public void success(String response, int id) {
                Gson gson = new Gson();
                Master master = gson.fromJson(response, Master.class);
                Message msg = Message.obtain();
                msg.what = 0;
                msg.obj = master;
                handler.sendMessage(msg);
            }
        });
    }

    @OnClick(R.id.rl_left)
    public void onViewClicked() {
        finish();
    }


    public void showToast(String str) {
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        CloseAllActivity.getScreenManager().popActivity(this);
    }
}
